home *** CD-ROM | disk | FTP | other *** search
- /* CPROG3.CPP - some examples of arithmetic on an integer variable */
-
- #include <stdio.h>
- #include <conio.h>
-
- void pause(void)
- {
- printf("\nPress a key...\n");
- while( kbhit() == 0)
- ;
- getch();
- }
-
-
- main()
- {
- int i; // Create local integer variable i
- clrscr(); // Clear the screen
- i=6; // Set i to value 6
- printf("i starts out as %d\n", i); // Print its value
- /* The %d in the string tells printf() to
- print a numeric value at that point. It
- expects to find it as a second parameter,
- the two parameters inside the brackets
- being separated by a comma. The d part of
- %d specifies that the number is to be
- shown as a decimal value. */
- i = i + 4; // Add 4 to i...
- printf("i+4=%d\n", i); // ...and print the new value
- i++; // Add 1 to i. i++ is a short-
- // hand way of saying i=i+1;
- printf("i++=%d\n", i);
- i *= 7; // i *= 7 is shorthand for i=i*7;
- // ...which would work equally as
- // well.
- printf("i*=7=%d\n",i);
- printf("i+=4=%d\n",i+=4); // Add 4 again. Note how i+=4 is
- // evaluated before being passed
- // through to %d
-
- i -= 4; // Subtraction: alternatively i=i-4;
- i /= 7; // Division: i=i/7; also works.
- // Other operators are available
- // such as modulo (%) - read about
- // them in the online help under
- // operators.
-
- pause(); // Wait for a keypress
- }
-